#include <allegro.h>
#include <stdio.h>

#define NB_IMAGES 3


typedef struct
{
int x,y;
int direction; // 0 = right , 1 = left 
BITMAP *img_right[NB_IMAGES];
BITMAP *img_left[NB_IMAGES];
BITMAP *img_face;
} MB;



//Background
BITMAP* load_background_bmp(char chemin_background[]);
void draw_background(BITMAP *Background,BITMAP*Buffer);
void destroy_background(BITMAP * Background);

//MB
void load_MB(MB *mini);
void destroy_MB(MB *mini);
void init_MB(MB *mini);

void move_MB_right(MB *mini,int *compteur,BITMAP * Background);
void move_MB_left(MB *mini,int *compteur,BITMAP * Background);
void move_MB(MB *mini,int *compteur,BITMAP * Background);

void draw_MB(MB mini,BITMAP *Buffer, int compteur);


int main() 
{

	char chemin_background[]="../images/background.bmp";

	BITMAP *Buffer;
	BITMAP *Background=NULL;
	
	MB mini;

	int compteur;

	/* General initialization function */
	allegro_init();
	if (install_keyboard() == -1) {
		allegro_message("Error! %s", allegro_error);
		return (-1);
	}
	set_color_depth(16);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 600, 800, 0, 0) != 0) {
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); /* here’s the command for text mode */
		allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
		return (1);
	}

	Buffer = create_bitmap(SCREEN_W, SCREEN_H);
	//loading images
	//loading MB
	load_MB(&mini);
	//loading background + background collision
	Background=load_background_bmp(chemin_background);
	//loading background collision Map


	//init position & direction
	compteur = 0;
	init_MB(&mini);

	while (!key[KEY_ESC]) {

	//vider le buffer
		clear_bitmap(Buffer);

	//moving MB
		move_MB(&mini,&compteur,Background);

	//drawing background
		draw_background(Background,Buffer);

	//drawing MB
		draw_sprite(Buffer, mini.img_face, mini.x, mini.y);
		//draw_MB(mini,Buffer,compteur);

	//blit Buffer to screen
		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

		if (compteur == 10000)
			compteur = 0;
	}
	//destroy MB
	destroy_MB(&mini);
	destroy_background(Background);
	destroy_bitmap(Buffer);
	return (0);
}
END_OF_MAIN();


//Background


BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
	if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);
		return (NULL);
	}
	return (Background);

}



void draw_background(BITMAP *Background,BITMAP*Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}


void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}


//MB
 

void load_MB(MB *mini)
{
	int i;
	char buf[50];
	for (i = 0; i < NB_IMAGES; i++) {
		sprintf(buf, "../images/right%d.bmp", i);
		mini->img_right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "../images/left%d.bmp", i);
		mini->img_left[i] = load_bitmap(buf, NULL);
	}
	
mini->img_face=load_bitmap("../images/face.bmp", NULL);

}

void destroy_MB(MB *mini)
{
	int i;
	for (i = 0; i < NB_IMAGES; i++) {
		destroy_bitmap(mini->img_right[i]);
		destroy_bitmap(mini->img_left[i]);
	}

}
void init_MB(MB *mini)
{
	mini->direction = 0;
	mini->x = 0;
	mini->y =600;
}

void move_MB_right(MB *mini,int *compteur,BITMAP * Background)
{ 

	if ( (mini->x) < (SCREEN_W - mini->img_right[0]->w) && (mini->direction)==0 )
	 {
		*compteur+=1;
		(mini->x)+=1;
		usleep(4000);
		mini->direction=0;
	}

		if((mini->x) == SCREEN_W - mini->img_right[0]->w -5)
		{mini->direction = 1;} //changer vers la gauche
}


void move_MB_left(MB *mini, int *compteur, BITMAP *Background)
{

	if  (mini->x > 0 + mini->img_left[1]->w && mini->direction==1) 
	{

	*compteur+=1; // si le compteur = 10 000 il sera réinitialisé à 0
	(mini->x)-=1;
	usleep(4000);
	mini->direction=1;

	}

	if  ( (mini->x) == ( 0 + mini->img_left[1]->w))
	{mini->direction = 0;} // changer vers la droite
}



void move_MB(MB *mini,int *compteur,BITMAP * Background)
{ 

	move_MB_right(mini, compteur, Background);

	move_MB_left(mini, compteur, Background);

}


void draw_MB(MB mini,BITMAP *Buffer, int compteur)
{
	static int last_direction;

	if (mini.direction == 0)//moving right
	{ 
		draw_sprite(Buffer, mini.img_right[compteur % (NB_IMAGES-1) ],mini.x, mini.y);
		last_direction=0;
	}

	if(mini.direction == 1) //moving left
	{
		draw_sprite(Buffer, mini.img_left[compteur % (NB_IMAGES-1) ],mini.x, mini.y);
		last_direction=1;
	}

}

